package cn.schoolwow.quickdao.module.database.ddl.common.flow.v2;

import cn.schoolwow.quickdao.domain.database.parent.DatabaseType;
import cn.schoolwow.quickdao.domain.entity.common.TableColumn;
import cn.schoolwow.quickdao.domain.provider.DatabaseProvider;
import cn.schoolwow.quickflow.domain.FlowContext;
import cn.schoolwow.quickflow.flow.BusinessFlow;

public class GetAutoIncrementStatementV2CommonFlow implements BusinessFlow {
    @Override
    public void executeBusinessFlow(FlowContext flowContext) throws Exception {
        TableColumn tableColumn = flowContext.checkInstanceData(TableColumn.class);
        StringBuilder sqlBuilder = flowContext.checkInstanceData(StringBuilder.class);
        DatabaseType databaseType = flowContext.checkInstanceData(DatabaseType.class);
        DatabaseProvider databaseProvider = flowContext.checkInstanceData(DatabaseProvider.class);

        switch (databaseType){
            case SQLite:{
                sqlBuilder.append(tableColumn.column + " " + tableColumn.columnType + (null == tableColumn.range ? "" : "(" + tableColumn.range + ")") + " primary key autoincrement");
            }break;
            case H2:
            case Mysql:
            case MariaDB:{
                sqlBuilder.append(tableColumn.column + " " + tableColumn.columnType + (null == tableColumn.range ? "" : "(" + tableColumn.range + ")") + " primary key auto_increment");
            }break;
            case Postgresql:
            case Oracle:{
                sqlBuilder.append(databaseProvider.escape(tableColumn.column) + " BIGSERIAL UNIQUE PRIMARY KEY");
            }break;
            case SQLServer:{
                sqlBuilder.append(tableColumn.column + " " + tableColumn.columnType + (null == tableColumn.range ? "" : "(" + tableColumn.range + ")") + " identity(1,1) unique ");
            }break;
        }
    }

    @Override
    public String name() {
        return "获取自增id字段语句-v2版本";
    }
}
